package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsPageStatisticsDao;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsUserOperationLogDao;
import com.syni.mdd.yhd.common.all.dto.requestDto.operation.UserOperationReDto;
import com.syni.mdd.yhd.common.all.entity.BmsUserOperationLog;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.entity.bo.user.CheckUserBo;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserOperationLogService extends BaseService<BmsUserOperationLog, Integer> {

    @Autowired
    BmsUserOperationLogDao bmsUserOperationLogDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    BmsVideoService bmsVideoService;

    @Autowired
    BmsPageStatisticsService bmsPageStatisticsService;


    public Map<String,Object> recordUserOperation(UserOperationReDto userOperationReDto) {
        CheckUserBo checkUserBo = RequestParamUtils.checkUserBo();
        //默认使用设备标识
        if(checkUserBo.getCheckStatus().equals(ResultCode.CODE_NORMAL)){
            userOperationReDto.setUserId(checkUserBo.getUserId());
        }
        BmsUserOperationLog bmsUserOperationLog = new BmsUserOperationLog();
        BeanUtils.copyProperties(userOperationReDto,bmsUserOperationLog);
        //将这种页面按钮统计转移到页面统计表
        if(bmsUserOperationLog.getOperationType() >= 15 && bmsUserOperationLog.getOperationType() <= 500){
            bmsPageStatisticsService.addToPageStatistics(userOperationReDto);
            return ResultMapHelper.success(null,ResultMsg.HAND_SUCCESS);
        }
        if(userOperationReDto.getUserId() != null) {
            if (!userOperationReDto.getUserId().startsWith(BmsVideoService.visitorPrfix)) {
                bmsUserOperationLog.setUserId(Integer.parseInt(userOperationReDto.getUserId()));
            }
        }
        bmsUserOperationLog.setFromOrigin(Constants.USER_CLIENT);
        if(bmsUserOperationLog.getOperationType() == Constants.OPEARTION_TYPE){
            if(bmsUserOperationLog.getVideoId() != null){
                Optional<BmsVideo> bmsVideoOptional = bmsVideoService.getBaseDao().findById(bmsUserOperationLog.getVideoId());
                if(bmsVideoOptional.isPresent()){
                    BmsVideo bmsVideo = bmsVideoOptional.get();
                    bmsUserOperationLog.setFromOrigin(bmsVideo.getFromOrigin());
                    bmsUserOperationLog.setIsWatchComplete(Constants.COMMON_FLAG_FALSE_INT);
                    Double play = Double.parseDouble(bmsUserOperationLog.getVideoPlay());
                    if(bmsUserOperationLog.getVideoLength().intValue() == play.intValue()){
                        bmsUserOperationLog.setIsWatchComplete(Constants.COMMON_FLAG_TRUE_INT);
                    }
                    String addrCity = bmsVideo.getAddrCity();
                    String addrDistinct = bmsVideo.getAddrDistrict();
                    //记录进redis
                    if(StringUtils.isNotBlank(addrCity) && StringUtils.isNotBlank(addrDistinct)) {
                        redisTemplate.opsForZSet().incrementScore(String.format("%s%s:%s%s",Constants.REDIS_KEY_VIDEO_WATCH_NUM,bmsUserOperationLog.getUserId(),addrCity,addrDistinct),"video:" + bmsVideoOptional.get().getId(), 1);
                    }
                }
            }
        }else if(bmsUserOperationLog.getOperationType() == Constants.USER_STAY){
            bmsUserOperationLog.setFromOrigin(Constants.USER_CLIENT);
            if(userOperationReDto.getBusinessId() != null){
                bmsUserOperationLog.setFromOrigin(Constants.BUSINESS_CLIENT);
            }
        }else if(bmsUserOperationLog.getOperationType() == Constants.GROUP_BUY_STAY){
            if(bmsUserOperationLog.getUserId() == null){
                return ResultMapHelper.success(null,ResultMsg.HAND_SUCCESS);
            }
        }
        bmsUserOperationLog.setOperationTime(new Date());
        bmsUserOperationLogDao.save(bmsUserOperationLog);
        return ResultMapHelper.success(null,ResultMsg.HAND_SUCCESS);
    }

    @Override
    public BmsUserOperationLogDao getBaseDao() {
        return bmsUserOperationLogDao;
    }


    public Integer countUserAppStay(Integer userId,Date startTime,Date endTime){
        Integer ret = 0;
        Map<String,Object> conditionMap = new LinkedHashMap<>();
        conditionMap.put("userId",userId);
        conditionMap.put("operationType",13);
        conditionMap.put("operationTime_GTTE",startTime);
        conditionMap.put("operationTime_LTT",endTime);
        List<BmsUserOperationLog> bmsUserOperationLogs = bmsUserOperationLogDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsUserOperationLogs != null && bmsUserOperationLogs.size() > 0){
            for(BmsUserOperationLog bmsUserOperationLog : bmsUserOperationLogs){
                Integer stay = bmsUserOperationLog.getUserStay();
                if(stay > 10000){
                    stay = stay / 1000;
                }
                ret += stay;
            }
        }
        return ret;
    }
}
